package game.util;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * @author: Pomelo.Lee
 * @date: 2017年8月29日 下午1:05:58
 */
public class CommUtil {

	/**
	 * 是大陆手机号
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isPhone(String phone) {
		if (StrKit.isEmpty(phone)) {
			return false;
		}
		if (phone.length() != 11) {
			return false;
		}
		if (!phone.startsWith("1")) {
			return false;
		}
		return NumberUtils.isNumber(phone);
		/*
		 * String regExp =
		 * "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";
		 * Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(phone);
		 * return m.matches();
		 */
	}

	/**
	 * 4位验证码
	 * 
	 * @return
	 */
	public static String generateFourCode() {
		return RandomStringUtils.random(4, "0123456789");
	}

	/**
	 * 解析客户端ip地址
	 * 
	 * @param address
	 * @return
	 */
	public static String praseClientIp(String address) {
		if (StrKit.isEmpty(address)) {
			return "";
		}
		if (address.startsWith("/")) {
			address = address.substring(1);
		}
		if (address.indexOf(":") > -1) {
			address = address.substring(0, address.indexOf(":"));
		}
		return address;
	}

	/**
	 * 获取客户端ip地址
	 * 
	 * @param address
	 * @return
	 */
	public static String getIpAddr(String address) {
		return praseClientIp(address);
	}

	public static void main(String[] args) {
		String phone = "13761559632";
		System.out.println(isPhone(phone));
		System.out.println(generateFourCode());

	}

}
